<?php
class Default_Model_WordRootRepository {

	const PREFIXE = "PREFIX";
	const SUFFIX = "SUFFIX";
	const ROOT = "ROOT";
	
	protected $xml;
	public function __construct($data) {
		if (file_exists($data)) {
			$this->xml = new SimpleXMLElement($data, NULL, true);
		} else {
			exit('Failed to open ' . $data);
		}
	}
	/**
	 * retrive prefixes that prefixing by $word
	 * @param  $word string
	 * @return array
	 */
	public function prefixes($word) {
		$all = $this->xml->xpath("//section[@category='prefix']/group/token[@name]");
		//Zend_Debug::dump($all);
		$match_tokens = $this->find_match_tokens($all, $word, 'start_with');
		$results =  array();
		foreach($match_tokens as $name => $token) {
			$section_descriptions = $token->xpath("../../@description");
			$results[$name] = array("category" => Default_Model_WordRootRepository::PREFIXE,
                                                 "description" => (string)$section_descriptions[0], 
                                                 "name" => (string)$token['name'], 
                                                 "original" => (string)$token['original'], 
                                                 "translate" => (string)$token['translate']);
		}
		return $results;
	}

	public function start_with($src, $start) {
		//echo "is $src match to $start ? " . stripos($src, $start) . " \n";
		return stripos($src, $start) === 0;
	}

	public function end_with($src, $end) {
		$pattern = "/$end\b/i";
		return preg_match($pattern, $src) === 1;
	}
	
	public function index_of($src, $index) {
		return !(stripos($src, $index) === false);
	}

	public function find_match_tokens($all, $word, $search_rule) {
		$results = array();
		foreach($all as $each) {
			$name_attr = (string)$each["name"];
			$isMultiPrefixInOneLine = stripos($name_attr, ",");
			if ($isMultiPrefixInOneLine === false) {
				$matched =  $this->$search_rule($word, $name_attr);
				if ($matched) {
					$results[$name_attr] = $each;
				}
			} else {
				$names = split(",", $name_attr);
				foreach ($names as $name) {
					$matched =  $this->$search_rule($word, trim($name));
					if ($matched) {
						$results[$name_attr] = $each;
						break;
					}
				}
			}
		}
		return $results;
	}

	public function suffixes($word) {
		$all = $this->xml->xpath("//section[@category='suffix']/group/token[@name]");
		$match_tokens = $this->find_match_tokens($all, $word, 'end_with');
		$results =  array();
		foreach($match_tokens as $name => $token) {
			$section_descriptions = $token->xpath("../../@description");
			$results[$name] = array("category" => Default_Model_WordRootRepository::SUFFIX,
                                                 "description" => (string)$section_descriptions[0], 
                                                 "name" => (string)$token['name'], 
                                                 "original" => (string)$token['original'], 
                                                 "translate" => (string)$token['translate']);
		}
		return $results;
	}
	
	public function roots($word) {
		$all = $this->xml->xpath("//section[@category='stem']/group/token[@name]");
		$match_tokens = $this->find_match_tokens($all, $word, 'index_of');
		$results =  array();
		foreach($match_tokens as $name => $token) {
			$section_descriptions = $token->xpath("../../@description");
			$results[$name] = array("category" => Default_Model_WordRootRepository::ROOT,
                                                 "description" => (string)$section_descriptions[0], 
                                                 "name" => (string)$token['name'], 
                                                 "original" => (string)$token['original'], 
                                                 "translate" => (string)$token['translate']);
		}
		return $results;		
	}
}